//
//  HWHomeTableHeader.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/24.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWHomeTableHeader: UITableViewHeaderFooterView {
    
    var titleLabel:UILabel!
    var titleLabelTwo:UILabel!
    var detailLabel:UILabel!
    var moreLabel:UILabel!
    var moreArrow:UIImageView!
    var moreButton:UIButton!
    var title:String? {
        didSet {
            
            let blackDic: [NSAttributedString.Key: Any] = [
                .font: UIFont.init(name: "AaHouDiHei", size: 20*widthScale)!,
                .foregroundColor: UIColor.white,
                .strokeWidth: -4*widthScale,
                .strokeColor: UIColor.colorWithHexString(hexString: "#2E1806"),
            ]
            
            let orangeDic: [NSAttributedString.Key: Any] = [
                .font: UIFont.init(name: "AaHouDiHei", size: 20*widthScale)!,
                .foregroundColor: UIColor.white,
                .strokeWidth: -4*widthScale,
                .strokeColor: UIColor.colorWithHexString(hexString: "#F87706"),
            ]
            var attributedText: NSMutableAttributedString!
            var attributedTextTwo: NSMutableAttributedString!
            if (title?.count ?? 0) == 4 {
                attributedText = NSMutableAttributedString(string: String((title ?? "").prefix(2)), attributes: blackDic)
                attributedTextTwo = NSMutableAttributedString(string: String((title ?? "").suffix(2)), attributes: orangeDic)
//                attributedText.addAttributes(orangeDic, range: NSRange(location: 2, length: 2))
            } else {
                attributedText = NSMutableAttributedString(string: title ?? "", attributes: blackDic)
            }
            
            titleLabel.attributedText = attributedText
            titleLabelTwo.attributedText = attributedTextTwo
        }
    }
    
    var moreButtonClick:(()->())?
    class func headerOrFooterWithTableView(tableView:UITableView) -> (HWHomeTableHeader) {
        let reUse:String = "HWHomeTableHeader"
        
        var headerOrFooter = tableView.dequeueReusableHeaderFooterView(withIdentifier: reUse)
        
        if headerOrFooter == nil {
            headerOrFooter = HWHomeTableHeader.init(reuseIdentifier: reUse)
        }
        
        return headerOrFooter as! HWHomeTableHeader
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        initUI()
        initFrame()
    }
    
    
    func initUI() -> Void {
        backgroundView = UIView()
        contentView.backgroundColor = UIColor.white
        
        titleLabel = HWLabel.createBoldLabel(title: "推荐", color: "#FFFFFF", font: 20*widthScale)
        titleLabel.font = UIFont.init(name: "AaHouDiHei", size: 20*widthScale)!
        titleLabel.layer.shadowColor = UIColor.colorWithHexString(hexString: "#2E1806").cgColor
        titleLabel.layer.shadowOffset = CGSize(width: 0, height: 2*widthScale)
        titleLabel.layer.shadowOpacity = 1
        titleLabel.layer.shadowRadius = 0
        contentView.addSubview(titleLabel)
        
        titleLabelTwo = HWLabel.createBoldLabel(title: "推荐", color: "#FFFFFF", font: 20*widthScale)
        titleLabelTwo.font = UIFont.init(name: "AaHouDiHei", size: 20*widthScale)!
        titleLabelTwo.layer.shadowColor = UIColor.colorWithHexString(hexString: "#F87706").cgColor
        titleLabelTwo.layer.shadowOffset = CGSize(width: 0, height: 2*widthScale)
        titleLabelTwo.layer.shadowOpacity = 1
        titleLabelTwo.layer.shadowRadius = 0
        contentView.addSubview(titleLabelTwo)
        
        detailLabel = HWLabel.createLabel(title: "这周大家都在玩的爆款游戏", color: "#9E9E9E", font: 11*widthScale)
        contentView.addSubview(detailLabel)
        
        moreArrow = UIImageView()
        moreArrow.image = UIImage(named: "homeMoreArrow")
        contentView.addSubview(moreArrow)
        
        moreLabel = HWLabel.createLabel(title: "更多", color: "#FA8C08", font: 11*widthScale)
        contentView.addSubview(moreLabel)
        
        moreButton = UIButton(type: .custom)
        moreButton.addTarget(self, action: #selector(moreButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(moreButton)
    }
    
    func initFrame() -> Void {

        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(16*widthScale)
            make.top.equalTo(12*widthScale)
        }
        
        titleLabelTwo.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right)
            make.centerY.equalTo(titleLabel)
        }
        
        detailLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(7*widthScale)
            make.height.equalTo(11*widthScale)
        }
        
        moreArrow.snp.makeConstraints { make in
            make.right.equalTo(-13*widthScale)
            make.width.height.equalTo(14*widthScale)
            make.top.equalTo(titleLabel.snp.top)
        }
        
        moreLabel.snp.makeConstraints { make in
            make.right.equalTo(moreArrow.snp.left).offset(-4*widthScale)
            make.centerY.equalTo(moreArrow)
        }
        
        moreButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(moreLabel)
            make.right.equalTo(moreArrow)
        }
    }
    
    @objc func moreButtonClick(_:UIButton) -> Void {
        self.moreButtonClick?()
    }
    
}
